package engine.processing.data.provider;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import engine.processing.data.exceptions.OLAPEngineException;
import engine.processing.data.model.CubeDimension;
import engine.processing.data.model.CubeMeasure;

public class TestDataProvider implements DataProvider{

	private final static Logger logger = Logger.getLogger(TestDataProvider.class);
	
	String cubeName;
	String srcDataPath;
	int aggrCount;
	
	List <CubeDimension> dimensionList = new ArrayList<CubeDimension>();
	List <CubeMeasure> measureList = new ArrayList<CubeMeasure>();
	
	public TestDataProvider(String filePath) throws OLAPEngineException {
		InputStream in = null;
		try {
			in = this.getClass().getClassLoader().getResourceAsStream(filePath);
			load(in);
		} catch(Exception e) {
			logger.debug(e.getMessage(), e);
			throw new OLAPEngineException(e);
		} finally {
			if(in != null)
				try {
					in.close();
				} catch (IOException e) {
					logger.debug(e.getMessage(), e);
					throw new OLAPEngineException(e);
				}
		}
	}

	public void load(InputStream in) throws OLAPEngineException {
		try {
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			documentBuilderFactory.setIgnoringComments(true);
			documentBuilderFactory.setNamespaceAware(true);
			try {
				documentBuilderFactory.setXIncludeAware(true);
			} catch (UnsupportedOperationException e) {
				logger.error("Failed to set setXIncludeAware(true) for parser "	+ documentBuilderFactory + ":" + e, e);
			}
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(in);
			Element root = document.getDocumentElement();
			if(!"cube".equalsIgnoreCase(root.getTagName())) 
				throw new OLAPEngineException("Bad configuration file: top-level element is not <cube>");
			String cubeName = root.getAttribute("name");
			if(cubeName == null)
				throw new OLAPEngineException("Bad configuration file: <cube> element does not have 'name' attribute");
			
			String sourcePath = root.getAttribute("sourcePath");
			if(sourcePath == null)
				throw new OLAPEngineException("Bad configuration file: <cube> element does not have 'sourcePath' " +
						"attribute");
			
			String sAggregationsCount = root.getAttribute("aggregationsCount");
			if(sAggregationsCount == null)
				throw new OLAPEngineException("Bad configuration file: <cube> element does not have 'aggregationsCount' " +
						"attribute");
			
			NodeList dimensions = root.getElementsByTagName("dimension");
			for(int i = 0; i < dimensions.getLength(); i ++) {
				Element dimension = (Element)dimensions.item(i);
				String name = dimension.getAttribute("name");
				if(name == null)
					throw new OLAPEngineException("Bad configuration file: <dimension> element does not have " +
							"'name' attribute.");
				dimensionList.add(new CubeDimension(name));
			}
			NodeList measures = root.getElementsByTagName("measure");
			for(int i = 0; i < measures.getLength(); i ++) {
				Element measure = (Element)measures.item(i);
				String name = measure.getAttribute("name");
				if(name == null)
					throw new OLAPEngineException("Bad configuration file: <measure> element does not have " +
							"'name' attribute.");
				measureList.add(new CubeMeasure(name));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new OLAPEngineException(e);
		}
	}
	
	
	@Override
	public List<String> getHDFSDataURLsForLastDay() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDataLoadStatement() {
		// TODO Auto-generated method stub
		return null;
	}

	public static void main(String [] args) throws OLAPEngineException {
		TestDataProvider tdp = new TestDataProvider("ReportDataConf.xml");
		System.out.println(tdp.dimensionList);
		System.out.println(tdp.measureList);
	}
	
}
